﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;
using System.Security.Principal;
using System.Text;

namespace EBA.WindowsServices
{
    static class Channels
    {
        public static void RegisterIpcChannel(string name)
        {
            SecurityIdentifier sid = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
            NTAccount account = sid.Translate(typeof(NTAccount)) as NTAccount;

            IDictionary props = new Hashtable();
            props["portName"] = name;
            props["name"] = name;
            props["exclusiveAddressUse"] = false;
            props["authorizedGroup"] = account.Value;//允许所有账号运行的程序进行连接


            // BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider();

            BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider();
            serverProvider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;

            var channel = new IpcChannel(props, null, serverProvider);
            ChannelServices.RegisterChannel(channel, false);
        }
    }
}
